<?php
namespace app\index\controller;
use model\Order;
use think\Controller;
use think\View;

class LoginController extends Controller
{
	/*
	  前台登录
	  $name 账号
	  $pwd 密码
	*/
    public function index(){

        $where = ['username'=>input('name'),'password_hash'=>input('pwd')];
        $rs = db('member')->where($where)->select();
        if(!empty($rs))
        {
            $time = time();
            $data = ['last_time'=>$time];
            $update_info = db('member')->where($data)->update();
            if($update_info)
            {
                return json_encode(['code'=>1,'msg'=>'登录成功','rs'=>$rs], JSON_UNESCAPED_UNICODE);
            }
            else
            {
                return json_encode(['code'=>0,'msg'=>'登录失败，预期之外的错误',], JSON_UNESCAPED_UNICODE);
            }
        	
        }
        else
        {
        	return json_encode(['code'=>0,'msg'=>'登录失败'], JSON_UNESCAPED_UNICODE);
        }
    }
    /*
      前台注册
      json
      $username 账号
      $password_hash 密码
      mobile 手机
    */
    public function register()
    {   
       
        $time = time();
    	$data = ['username'=>isEmpty(input('name')),
                'password_hash'=>isEmpty(input('pwd')),
                'mobile'=>isEmpty(input('mobile')),
                'last_time'=>$time,
                'created_at'=>$time,
                'updated_at'=>$time,
            ];

        $rs = db('member')->insertGetId($data);
        if(!empty($rs))
        {
            //$userinfo = db('member')->where(['id'=>$rs])->find();
            if(!empty($userinfo))
            {
                return json_encode(['code'=>1,'msg'=>'注册成功','rs'=>$userinfo], JSON_UNESCAPED_UNICODE);
            }
            else
            {
                return json_encode(['code'=>0,'msg'=>'注册失败'], JSON_UNESCAPED_UNICODE);
            }
            
        }
        else
        {
            return json_encode(['code'=>0,'msg'=>'注册失败'], JSON_UNESCAPED_UNICODE);
        }
    }

    public function text()
    {
        return 11111;
    }
}
